package com.uxwine.alarm.mon;

import java.util.Calendar;

import com.nny.alarm.R;
import com.nny.alarm.more.AlarmBriefUtil;
import com.uxwine.alarm.Alarm;
import com.uxwine.alarm.AlarmCalc;
import com.uxwine.util.Log;

import android.annotation.TargetApi;
import android.app.KeyguardManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

/**
 * 管理闹钟相关的通知栏的显示
 * @author zhangzhongming
 * @date	2013.5.2
 */
public class AlarmNotify
{
	private Context mCtx;
	private NotificationManager mNotifyManager;
	private AlarmBriefUtil mBrief;
	
	static final int MAIN_ID = -1;
	
	public AlarmNotify(final Context ctx)
	{
		mCtx = ctx;
		mNotifyManager = (NotificationManager)
				mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
		
		mBrief = new AlarmBriefUtil(mCtx);
	}
	
	/**
	 * 当AlarmReceiver接收到ACTION_ALARM_ALERT广播后调用，以通知用户
	 * 闹钟到点。这里有一个FullScreenIntent的方法，这个方法在android-9
	 * 中才有。这个Intent主要是在锁屏或者全拼时，用户不用点击通知栏二弹出窗口
	 * 提示用户闹钟已经到点，该方法后续需要改进。
	 * 
	 * @author zhangzhongming
	 * */	
	
	//XXX:需要针对android-8改进
	@SuppressWarnings("deprecation")
	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	public void notify(Alarm alarm)
	{
		// Decide which activity to start based on the state of the keyguard.
		final AlarmContext ac = AlarmContext.getInstance(mCtx);
		Class c = ac.getAlertActivity(false);
		KeyguardManager km = (KeyguardManager) mCtx
				.getSystemService(Context.KEYGUARD_SERVICE);
		if (km.inKeyguardRestrictedInputMode())
		{
			// Use the full screen activity for security.
			c = ac.getAlertActivity(true);
		}
		
		// Trigger a notification that, when clicked, will show the alarm alert
		// dialog. No need to check for fullscreen since this will always be
		// launched from a user action.
		Intent notify = new Intent(mCtx, c);
		notify.putExtra(AlarmUtil.EXTRA_ALARM_INTENT, alarm);
		PendingIntent pendingNotify = PendingIntent.getActivity(mCtx,
				alarm.id, notify, 0);

		// Use the alarm's label or the default label as the ticker text and
		// main text of the notification.
		String label = "alarm :" + alarm.id; //alarm.getLabelOrDefault(context);
		Notification n = new Notification(ac.getAlartIcon(), label, alarm.trigger);
		n.setLatestEventInfo(mCtx, label, "begin alert!!", pendingNotify);
		n.flags |= Notification.FLAG_SHOW_LIGHTS
				| Notification.FLAG_ONGOING_EVENT;
		n.defaults |= Notification.DEFAULT_LIGHTS;

		// NEW: Embed the full-screen UI here. The notification manager will
		// take care of displaying it if it's OK to do so.
//		if(false && (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD))
//		{
//			Intent alarmAlert = new Intent(mCtx, c);
//			alarmAlert.putExtra(AlarmUtil.EXTRA_ALARM_INTENT, alarm);
//			alarmAlert.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
//					| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
//			n.fullScreenIntent = PendingIntent.getActivity(mCtx, alarm.id,
//					alarmAlert, 0);
//		}
		// Send the notification using the alarm id to easily identify the
		// correct notification.
		final NotificationManager nm = mNotifyManager;
		nm.notify(alarm.id, n);
	}
	
	@SuppressWarnings("deprecation")
	public void update(final Alarm alarm, final int timeout)
	{
		if (alarm == null)
		{
			Log.d("zhangzhongming",	"Cannot update notification for killer callback");
			return;
		}
		
		final AlarmContext ac = AlarmContext.getInstance(mCtx);
		// Launch SetAlarm when clicked.
		Intent viewAlarm = new Intent(mCtx, ac.getAlertActivity(false));
		//viewAlarm.putExtra(Alarms.ALARM_ID, alarm.id);
		PendingIntent intent = PendingIntent.getActivity(mCtx, alarm.id,
				viewAlarm, 0);

		// Update the notification to indicate that the alert has been
		// silenced.
		String label = "Alarm_label_update_timeout";
		Notification n = new Notification(ac.getAlartIcon(), label,
				alarm.trigger);
		n.setLatestEventInfo(
				mCtx,
				label,
				"Show Setting Notification",
				intent);
		n.flags |= Notification.FLAG_AUTO_CANCEL;
		// We have to cancel the original notification since it is in the
		// ongoing section and we want the "killed" notification to be a plain
		// notification.
		mNotifyManager.cancel(alarm.id);
		mNotifyManager.notify(alarm.id, n);
	}
	
	public void notifySnoozed(final Alarm alarm)
	{
		final AlarmContext ac = AlarmContext.getInstance(mCtx);

		// Launch SetAlarm when clicked.
		Intent viewAlarm = new Intent(mCtx, ac.getAlertActivity(false));
		//viewAlarm.putExtra(Alarms.ALARM_ID, alarm.id);
		PendingIntent intent = PendingIntent.getActivity(mCtx, alarm.id,
				viewAlarm, 0);
		Notification n = new Notification(ac.getAlartIcon(), "cancel snooze", 0);
		n.setLatestEventInfo(mCtx, "cancel snooze", "time", intent);
		
		n.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;
		mNotifyManager.notify(alarm.id, n);	
	}
	
	public void notifySnoozedx(final Alarm alarm)
	{
		final AlarmContext ac = AlarmContext.getInstance(mCtx);
		
		Intent cancelSnooze = new Intent(mCtx, AlarmReceiver.class);
		cancelSnooze.setAction(AlarmUtil.ACTION_SNOOZE_CANCEL);
		cancelSnooze.putExtra(AlarmUtil.EXTRA_ALARM_ID, alarm.id);
		
		PendingIntent broadcast = PendingIntent.getBroadcast(mCtx, alarm.id, cancelSnooze, 0);
		Notification n = new Notification(ac.getAlartIcon(), "cancel snooze", 0);
		n.setLatestEventInfo(mCtx, "cancel snooze", "time", broadcast);
		n.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONGOING_EVENT;
		mNotifyManager.notify(alarm.id, n);	
	}
	
	public void cancel(final int id)
	{
		mNotifyManager.cancel(id);
	}
	
	public void updateStated(Alarm alarm, int state)
	{
		if (alarm == null)
		{
			Log.d("zhangzhongming",	"Cannot update notification for killer callback");
			mNotifyManager.cancel(MAIN_ID);
			return;
		}
		//表示常驻提醒
		if(state == 0)
		{
			Notification n = createStatedIntent(alarm);
			mNotifyManager.notify(MAIN_ID, n);			
		}
		//开始闹的时候
		else if(state == 1)
		{
			Notification n = createAlertIntent(alarm);
			mNotifyManager.notify(MAIN_ID, n);	
		}
		//处在贪睡模式的时候。
		else if(state == 2)
		{
			Notification n = createSnoozeIntent(alarm);
			mNotifyManager.notify(MAIN_ID, n);	
		}
	}
	
	private Notification createStatedIntent(Alarm alarm)
	{
		final AlarmContext ac = AlarmContext.getInstance(mCtx);
		Intent viewMain = new Intent(mCtx, ac.getMainActivity());
		PendingIntent intent = PendingIntent.getActivity(mCtx, MAIN_ID, viewMain, 0);
		
		
		String brief = alarm.label + "(" + mBrief.getCycleString(alarm.cycle) + ")";
		final Calendar trigger = AlarmCalc.triggerTime(alarm.time, alarm.cycle);
		String label = mBrief.getTriggerDetail(alarm.cycle, trigger);
		
		Notification n = new Notification();
		n.icon = ac.getAlartIcon();
		n.tickerText = label;
		n.setLatestEventInfo(mCtx, label, brief, intent);
		n.flags |= Notification.FLAG_NO_CLEAR;
		return n;
	}
	
	private Notification createAlertIntent(Alarm alarm)
	{
		final AlarmContext ac = AlarmContext.getInstance(mCtx);
		Intent viewMain = new Intent(mCtx, ac.getAlertActivity(false));
		PendingIntent intent = PendingIntent.getActivity(mCtx, MAIN_ID, viewMain, 0);
		
		
		String label = "点击进入闹钟设置界面";
		String brief = alarm.label + "(" + mBrief.getCycleString(alarm.cycle) + ")";		
		
		Notification n = new Notification();
		n.icon = ac.getAlartIcon();
		n.tickerText = label;
		n.setLatestEventInfo(mCtx, label, brief, intent);
		n.flags |= Notification.FLAG_NO_CLEAR;
		return n;
	}
	
	private Notification createSnoozeIntent(Alarm alarm)
	{
		final AlarmContext ac = AlarmContext.getInstance(mCtx);
		Intent viewMain = new Intent(mCtx, ac.getAlertActivity(false));
		PendingIntent intent = PendingIntent.getActivity(mCtx, MAIN_ID, viewMain, 0);
		
		
		String label = "五分钟后再次提醒，点击取消或者推迟";
		String brief = alarm.label + "(" + mBrief.getCycleString(alarm.cycle) + ")";
		
		Notification n = new Notification();
		n.icon = ac.getAlartIcon();
		n.tickerText = label;
		n.setLatestEventInfo(mCtx, label, brief, intent);
		n.flags |= Notification.FLAG_NO_CLEAR;
		return n;
	}
}
